package com.renren.webmaven2.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.renren.eliya.domain.BigClass;
import com.renren.eliya.domain.BreadPic;
import com.renren.eliya.domain.BreadPrice;
import com.renren.eliya.domain.MyBread;
import com.renren.eliya.domain.MyComment;
import com.renren.eliya.domain.SmallClass;
import com.renren.webmaven2.service.BigClassService;
import com.renren.webmaven2.service.BreadService;
import com.renren.webmaven2.service.SmallClassService;
import com.renren.webmaven2.util.CacheUtils;
import com.renren.webmaven2.util.Constants;
import com.renren.yx.base.dao.daoutil.InitDao;

/**
 */
@Controller
@SuppressWarnings("unchecked")
public class Home2Controller extends LocalBaseController {

	@Autowired
	private SmallClassService smallClassService;

	@Autowired
	private BigClassService bigClassService;

	@Autowired
	private BreadService breadService;

	@RequestMapping(value = { "/", "index.html" })
	public String index(HttpServletRequest request, Model model) {
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);
		List<SmallClass> smallClasses = smallClassService.findByIndexView(1);
		Map<Integer, List<MyBread>> map = breadService.getBreadsMap(smallClasses);
		model.addAttribute("bigClasses", bigClasses);
		model.addAttribute("smallClasses", smallClasses);
		model.addAttribute("breadMap", map);
		model.addAttribute("isIndex", 1);
		logger.info("index");
		return "index";
	}

	@RequestMapping(value = { "aboutUs.html" })
	public String aboutUs(HttpServletRequest request, Model model) {
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);

		model.addAttribute("bigClasses", bigClasses);
		logger.info("aboutUs");
		return "aboutUs";
	}

	@RequestMapping(value = { "guide.html" })
	public String guide(HttpServletRequest request, Model model) {
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);

		model.addAttribute("bigClasses", bigClasses);
		logger.info("guide");
		return "guide";
	}

	@RequestMapping(value = { "send.html" })
	public String send(HttpServletRequest request, Model model) {
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);

		model.addAttribute("bigClasses", bigClasses);
		logger.info("send");
		return "send";
	}

	@RequestMapping(value = { "contact.html" })
	public String contact(HttpServletRequest request, Model model) {
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);

		model.addAttribute("bigClasses", bigClasses);
		logger.info("contact");
		return "contact";
	}

	@RequestMapping(value = { "job.html" })
	public String job(HttpServletRequest request, Model model) {
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);

		model.addAttribute("bigClasses", bigClasses);
		logger.info("job");
		return "job";
	}

	@RequestMapping(value = { "detail.html" })
	public String detail(HttpServletRequest request, Model model) {

		String id = request.getParameter("id");
		MyBread bread = (MyBread) InitDao.getDao().getEntity(MyBread.class, Integer.valueOf(id));
		SmallClass smallClass = smallClassService.get(bread.getSmallClassId());
		List<BreadPic> breadPics = breadService.findBreadPicsByBreadId(bread.getId());
		BigClass bigClass = (BigClass) InitDao.getDao().getEntity(BigClass.class, bread.getBigClassId());
		List<MyComment> comments = breadService.findCommentsByBreadId(bread.getId());
		List<BreadPrice> breadPrices = breadService.findBreadPricesByBreadId(bread.getId());
		model.addAttribute("smallClass", smallClass);
		model.addAttribute("bigClass", bigClass);
		model.addAttribute("bread", bread);
		model.addAttribute("breadPics", breadPics);
		model.addAttribute("comments", comments);
		model.addAttribute("breadPrices", breadPrices);

		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);
		model.addAttribute("bigClasses", bigClasses);

		logger.info("detail");
		return "detail";
	}

	@RequestMapping(value = { "bigClassPage.html" })
	public String bigClassPage(HttpServletRequest request, Model model) {
		String id = request.getParameter("id");
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);
		List<SmallClass> smallClasses = smallClassService.findByBigClass(Integer.valueOf(id));
		Map<Integer, List<MyBread>> map = breadService.getBreadsMap(smallClasses);

		model.addAttribute("bigClassesId", id);
		model.addAttribute("bigClasses", bigClasses);
		model.addAttribute("smallClasses", smallClasses);
		model.addAttribute("breadMap", map);
		logger.info("bigClassPage:" + id);
		return "index";
	}

	@RequestMapping(value = { "smallClassPage.html" })
	public String smallClassPage(HttpServletRequest request, Model model) {
		String id = request.getParameter("id");
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);
		SmallClass smallClass = (SmallClass) InitDao.getDao().getEntity(SmallClass.class, Integer.valueOf(id));
		List<SmallClass> smallClasses = new ArrayList<SmallClass>();
		smallClasses.add(smallClass);
		Map<Integer, List<MyBread>> map = breadService.getBreadsMap(smallClasses);
		model.addAttribute("bigClasses", bigClasses);
		model.addAttribute("smallClasses", smallClasses);
		model.addAttribute("breadMap", map);
		logger.info("smallClassPage:" + id);
		return "index";
	}

	@RequestMapping(value = { "speak.html" })
	public String speak(HttpServletRequest request, Model model) {
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);
		model.addAttribute("bigClasses", bigClasses);
		return "speak";
	}

	@RequestMapping(value = { "joinus.html" })
	public String joinus(HttpServletRequest request, Model model) {
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);
		model.addAttribute("bigClasses", bigClasses);
		return "joinus";
	}

}
